#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "timer.h"
#include "input.h"
#include "mygrafx.h"

#include "titlescreen.h"
#include "level1.h"
#include "level2.h"
#include "endlevel.h"
#include "stages.h"

#include <cstdlib>
#include <iostream>

// 2340 LOC as of 2-23-10
using namespace std;

SDL_Surface* GlobalScreen::screen;

// time to change shit o ya.

int main( int argc, char* args[] )
{

    input game_input;
    timer time;

    //create the gamesurface where everything is drawn
    MyGrafx gamesurface;
    gamesurface.setup("Awesome Game");

    //Set up a Levels pointer.  Put's TitleScreen in as the opening Level
    Levels * current_level;
    current_level = new TitleScreen;

    Stages *stage_information;

    int level_number = 0;

    int health;// = 600;
    int score =0;

    bool quit = false;
    while( quit == false )
    {
        while(!time.checkTime() )
        {}
        gamesurface.clear();
        current_level->draw();
        gamesurface.flip();

        //Get input from Keyboard and Joystick
        if( true == game_input.perform_input(current_level))
        {
            quit = true;
            delete current_level;
            current_level = NULL;
        }

        //Temp code.  Used to load the next level when current level has ended.
        if( current_level != NULL && false == current_level->check_level_alive())
        {
            // if restart_level == false, level is still over
            // move to the next level
            if( false == current_level->check_restart_level())
            {
               level_number++;

                //Show inbetween stages
                health = current_level->getPlayerHealth();
                delete current_level;
                current_level = NULL;

                stage_information = new Stages();
                stage_information->draw(level_number);
                delete stage_information;
                stage_information = NULL;

            }
            if( NULL != current_level && true == current_level->check_restart_level() )
            {
                //Show inbetween stages
                Player super_tmp;
                health = super_tmp.DEFAULT_P_HEALTH;
                delete current_level;
                current_level = NULL;

                stage_information = new Stages();
                stage_information->draw( 4 ); // 4 is player death
                delete stage_information;
                stage_information = NULL;

            }

            if( level_number == 1 )
                current_level = new Level1(health, score);
            else if( level_number == 2 )
                current_level = new Level2(health, score);
            else if( level_number == 3 )
                current_level = new EndLevel(health, score);
            else
                quit = true;
        }

    }//end of main while loop

    TTF_Quit();
    SDL_Quit();

    return 0;
}
